﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace States
{
    public class State
    {
        // Attribute Data members
        private String stateCode;
        public String StateCode
        {
            get { return stateCode; }
            set { stateCode = value; }
        }

        private String stateName;
        public String StateName
        {
            get { return stateName; }
            set { stateName = value; }
        }

        private String nickname;
        public String NickName
        {
            get { return nickname; }
            set { nickname = value; }
        }

        private int population;
        public int Population
        {
            get { return population; }
            set { population = value; }
        }

        // Relationship Data Members
        // Capital city
        private City capitalCity;
        public City CapitalCity
        {
            get { return capitalCity; }
            set { capitalCity = value; }
        }

        // Set of cities
        private List<City> cities;
        public List<City> Cities
        {
            get { return cities; }
            set { cities = value; }
        }

        public State()
        {
            
        }

        public State(string sText)
        {
            if (!string.IsNullOrEmpty(sText))
            {
                string[] M = sText.Split(new char[] { ':' }, StringSplitOptions.RemoveEmptyEntries);
                StateCode = M[0];
                StateName = M[1];
                NickName = M[2];
                int nTemp;
                if (int.TryParse(M[3], out nTemp))
                {
                    Population = nTemp;
                }
            }
        }

        public void SetCities(string sText)
        {
            if (!string.IsNullOrEmpty(sText))
            {
                string[] M = sText.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
                for (int i = 0; i < M.Length; i++)
                {
                    string m = M[i];
                    if (!string.IsNullOrEmpty(m))
                    {
                        City city = new City(m) { State = this };

                        if (i == 0) 
                            CapitalCity = city;

                        if (cities == null) 
                            cities = new List<City>();
                        cities.Add(city);
                    }
                }
            }
        }

        public override string ToString()
        {
            return string.Format("State Code:'{0}' - Name:'{1}' - Nick Name:'{2}' - Population:{3}", StateCode, StateName, NickName, Population );
        }
    }
}
